﻿using Caliburn.Micro;
using myapp.Configs;
using myapp.ViewModels;
using myappcore.ViewModels;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace myapp.Instances
{
    /// <summary>
    /// Main Instance
    /// </summary>
    public class MainInstance
    {

        #region Attribute

        /// <summary>
        /// Static Instance
        /// </summary>
        private static MainInstance _instance;

        private List<IFunctionViewModel> _functionViewModelList;

        /// <summary>
        /// Logger
        /// </summary>
        private static readonly ILog Logger = LogManager.GetLog(typeof(MainInstance));

        #endregion

        #region Properties

        /// <summary>
        /// Get current EventAggregatorInstance
        /// </summary>
        public static MainInstance Instance
        {
            get
            {
                if (null == _instance)
                {
                    _instance = new MainInstance();
                }
                return _instance;
            }
        }

        /// <summary>
        /// EventAggregator
        /// </summary>
        public IEventAggregator EventAggregator { get; set; }

        /// <summary>
        /// Window Manager
        /// </summary>
        public IWindowManager WindowManager { get; set; }

        /// <summary>
        /// Color config
        /// </summary>
        public ColorConfig ColorConfig { get; set; }

        /// <summary>
        /// Template ViewModel List
        /// </summary>
        public List<ITemplateViewModel> TemplateViewModelList { get; set; }

        /// <summary>
        /// Function ViewModel List
        /// </summary>
        public List<IFunctionViewModel> FunctionViewModelList
        {
            get
            {
                return _functionViewModelList;
            }
        }

        #endregion

        #region Constructor

        private MainInstance()
        {
            
            ColorConfig = new ColorConfig();
            _functionViewModelList = new List<IFunctionViewModel>();
           
        }


        #endregion

        #region Method

        /// <summary>
        /// Get Template ViewModel By Code
        /// </summary>
        /// <param name="modelViewCode"></param>
        /// <returns></returns>
        public ITemplateViewModel ITemplateViewModelByCode(string modelViewCode)
        {
            //Logger.Debug("Begin get ITemplateViewModel by Code");
            if (null != TemplateViewModelList && TemplateViewModelList.Count > 0)
            {
                return TemplateViewModelList.Single(s => s.TemplateCode.ToLower() == modelViewCode.ToLower());
            }
            return null;
        }

        /// <summary>
        /// Function ViewModel By Code
        /// </summary>
        /// <param name="modelViewCode"></param>
        /// <returns></returns>
        public Screen ActionViewModelByCode(string modelViewCode)
        {
            //Logger.Debug("Begin get IFunctionViewModel by Code");
            if (null != _functionViewModelList && _functionViewModelList.Count > 0)
            {
                return _functionViewModelList.Single(s => s.Code.ToLower() == modelViewCode.ToLower()) as Screen;
            }
            return null;
            //return new PossitonManagerViewModel();
        }

        /// <summary>
        /// Init plugin
        /// </summary>
        private void InitPlugin()
        {
            foreach (Assembly assembly in AssemblySource.Instance)
            {
                foreach (Type type in assembly.GetTypes())
                {
                    if (type.IsClass && typeof(IFunctionViewModel).IsAssignableFrom(type))
                    {
                        IFunctionViewModel function = Activator.CreateInstance(type) as IFunctionViewModel;
                        _functionViewModelList.Add(function);
                    }
                }
            }

        }
        #endregion

    }
}
